#include "dialogconfig.h"
#include "ui_dialogconfig.h"

#include "appsettings.h"
#include "appconst.h"
#include "Account.h"



DialogConfig::DialogConfig(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogConfig)
{
    ui->setupUi(this);

    ui->comboBoxLayout->addItem("1");
    ui->comboBoxLayout->addItem("4");
    ui->comboBoxLayout->addItem("9");
    ui->comboBoxLayout->addItem("16");

	ui->comboBoxDuration->addItem(QString("%1").arg(10));
	ui->comboBoxDuration->addItem(QString("%1").arg(20));
	ui->comboBoxDuration->addItem(QString("%1").arg(30));
	ui->comboBoxDuration->addItem(QString("%1").arg(40));
	ui->comboBoxDuration->addItem(QString("%1").arg(50));
	ui->comboBoxDuration->addItem(QString("%1").arg(60));
	ui->comboBoxDuration->addItem(QString("%1").arg(90));
	ui->comboBoxDuration->addItem(QString("%1").arg(120));
	
    showConfig();

}

DialogConfig::~DialogConfig()
{
    delete ui;
}

void DialogConfig::setUser(const QString& user)
{
    bool enabled = Account::isSuperAdmin(user);
    ui->lineEditTitle->setEnabled(enabled);
}

void DialogConfig::on_pushButtonFreq_clicked()
{
    emit clickClearFreq();
}

void DialogConfig::on_pushButtonProgram_clicked()
{
    emit clickClearProgram();
}

void DialogConfig::on_pushButtonLoop_clicked()
{
    emit clickClearLoop();
}

void DialogConfig::on_pushButtonReset_clicked()
{
    AppSettings::set("App/Title", "");
    AppSettings::setInt("Loop/Layout", AppConst::LOOP_LAYOUT);
    AppSettings::setInt("Loop/Duration", AppConst::LOOP_DURATION);

    showConfig();

    emit clickReset();
}

void DialogConfig::on_pushButtonOK_clicked()
{
	QString title = ui->lineEditTitle->text();
	AppSettings::set("App/Title", title);

	QString layout = ui->comboBoxLayout->currentText();
	AppSettings::set("Loop/Layout", layout);

	QString duration = ui->comboBoxDuration->currentText();
	AppSettings::set("Loop/Duration", duration);

    accept();
}

void DialogConfig::on_pushButtonCancel_clicked()
{
    reject();
}

void DialogConfig::showConfig()
{
    QString  title = AppSettings::getString("App/Title", "");
    ui->lineEditTitle->setText(title);

    int layout = AppSettings::getInt("Loop/Layout", AppConst::LOOP_LAYOUT);
    ui->comboBoxLayout->setCurrentText(QString("%1").arg(layout));

    int duration = AppSettings::getInt("Loop/Duration", AppConst::LOOP_DURATION);
    ui->comboBoxDuration->setCurrentText(QString("%1").arg(duration));
}
